package com.sc.nft.admin.controller;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import com.sc.nft.admin.request.AdminDaoEcologyPointsWalletLogPageRequest;
import com.sc.nft.admin.request.AdminEditDaoPointsRequest;
import com.sc.nft.admin.request.DaoEcologyPointsRequest;
import com.sc.nft.admin.request.ManagementRecordPageRequest;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.entity.vo.AdminDaoEcologyPointsVO;
import com.sc.nft.entity.vo.AdminDaoEcologyPointsWalletLogPageVO;
import com.sc.nft.entity.vo.AdminDaoEcologyPointsWalletVO;
import com.sc.nft.entity.vo.AdminManagementRecordPageVO;
import com.sc.nft.enums.DaoEcologyPointsWalletLogTypeEnum;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.DaoEcologyPointsService;
import com.sc.nft.service.DaoEcologyPointsWalletService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.concurrent.TimeUnit;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/dao/ecology/points/wallet")
@Api(tags = "后台用户积分管理")
public class AdminDaoEcologyPointsWalletLogController {

    private final DaoEcologyPointsWalletService daoEcologyPointsWalletService;
    private final Redisson redisson;

    @PostMapping(value = "/getPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "分页获取DAO积分列表", notes = "分页获取DAO积分列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AdminDaoEcologyPointsWalletVO> getPage(@Valid @RequestBody DaoEcologyPointsRequest request) {
        return PageResult.success(daoEcologyPointsWalletService.getPage(request.getDaoName(),request.getPageNo(),request.getPageSize()));
    }

    @PostMapping(value = "/editDaoPoints", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "编辑DAO生态积分", notes = "编辑用户生态积分", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result editDaoPoints(@Valid @RequestBody AdminEditDaoPointsRequest request) {
        RLock lock = redisson.getLock(RedisKeyEnum.EDIT_DAO_POINTS_LOCK.getKey() + request.getDaoId());
        try {
            boolean res = lock.tryLock(3, TimeUnit.SECONDS);
            if (res) {
                daoEcologyPointsWalletService.editDaoPoints(request.getDaoId(),request.getPointsId(),request.getPointsName(),
                        Convert.toBigDecimal(request.getAmount()),
                        request.getDaoEcologyPointsWalletLogTypeEnum(),request.getRemark());
            }else {
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (InterruptedException e) {
            throw new GlobalRunTimeException("系统超时");
        } finally {
            lock.unlock();
        }
        return Result.success();
    }

    @PostMapping(value = "/managementRecordPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "DAO积分列表明细", notes = "DAO积分列表明细", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<AdminDaoEcologyPointsWalletLogPageVO> adminDaoEcologyPointsWalletLogPage(@Valid @RequestBody AdminDaoEcologyPointsWalletLogPageRequest request) {
        return PageResult.success(daoEcologyPointsWalletService.adminDaoEcologyPointsWalletLogPage(request.getDaoId(),request.getWalletId(),request.getType(),request.getStartTime(),request.getEndTime(),request.getPageNo(),request.getPageSize()));
    }



}
